package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.condition.ContractStatusCondition;
import com.ruicar.afs.cloud.basic.common.entity.BasicAssetsRepaymentPlan;
import com.ruicar.afs.cloud.basic.common.service.BasicAssetsRepaymentPlanService;
import com.ruicar.afs.cloud.basic.common.vo.ExpenseVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xudongdong
 * @date: 2020/7/15
 * @description: 合同资产还款计划controller
 */
@Slf4j
@RestController
@RequestMapping("/assetsRepaymentPlan")
@AllArgsConstructor
public class BasicAssetsRepaymentPlanController {

    private final BasicAssetsRepaymentPlanService basicAssetsRepaymentPlanService;

    @ApiOperation(value = "更新状态")
    @PostMapping("/updateStatus")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> updateStatus(@RequestBody ContractStatusCondition contractStatus) {
        basicAssetsRepaymentPlanService.update(Wrappers.<BasicAssetsRepaymentPlan>lambdaUpdate().eq(BasicAssetsRepaymentPlan::getContractNo, contractStatus.getContractNo())
                .set(BasicAssetsRepaymentPlan::getStatus, contractStatus.getContractStatus()));
        return new IResponse().setData(true);
    }

    @ApiOperation(value = "根据合同号查询差额租金信息")
    @PostMapping("/getAssetsRepaymentList")
    public IResponse getAssetsRepaymentList(@RequestParam("contractNo") String contractNo) {
        List<BasicAssetsRepaymentPlan> list = basicAssetsRepaymentPlanService.list(Wrappers.<BasicAssetsRepaymentPlan>lambdaUpdate()
                .eq(BasicAssetsRepaymentPlan::getContractNo, contractNo));
        List<ExpenseVo> expenseVoList = new ArrayList<>();
        for (BasicAssetsRepaymentPlan assetsRepaymentPlan : list) {
            if (assetsRepaymentPlan.getReceivableRent().compareTo(assetsRepaymentPlan.getActualRent()) != 0) {
                ExpenseVo ExpenseVo = new ExpenseVo();
                ExpenseVo.setTermNo(assetsRepaymentPlan.getTermNo());
                ExpenseVo.setRent(assetsRepaymentPlan.getReceivableRent().subtract(assetsRepaymentPlan.getActualRent()));
                ExpenseVo.setInterest(assetsRepaymentPlan.getReceivableInterest().subtract(assetsRepaymentPlan.getActualInterest()));
                ExpenseVo.setSubtotal(ExpenseVo.getRent().add(ExpenseVo.getInterest()));
                expenseVoList.add(ExpenseVo);
            }

        }
        return new IResponse().setData(expenseVoList);
    }

}
